package com.twxm.breed.service;

import com.alibaba.fastjson.JSONObject;
import com.twxm.breed.entity.FishPond;
import com.twxm.breed.entity.WaterFactor;
import com.twxm.breed.mapper.FishPondMapper;
import com.twxm.breed.mapper.WaterFactorMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class WaterFactorService {
    @Autowired
    private WaterFactorMapper waterFactorMapper;

    @Autowired
    private FishPondMapper fishPondMapper;

    @Transactional(rollbackFor = Exception.class)
    public void insertList(List<HashMap<String,String>> list) throws Exception {
        for (int i = 0; i< list.size(); ++i){
            Date date = new Date();
            float waterTemperature = 0;
            float PH = 0;
            float ammoniaNitrogen = 0;
            float totalNitrogen = 0;
            float totalPhosphorus = 0;
            float permanganate = 0;
            float nitrite = 0;
            String pondId = "";
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            for (String key: list.get(i).keySet()){
                if (key.equals("水温")||key.equals("waterTemperature")){
                    waterTemperature = Float.parseFloat(list.get(i).get(key));
                }else if(key.equals("PH")){
                    PH = Float.parseFloat(list.get(i).get(key));
                }else if(key.equals("氨氮")||key.equals("ammoniaNitrogen")){
                    ammoniaNitrogen = Float.parseFloat(list.get(i).get(key));
                }else if(key.equals("总氮")||key.equals("totalNitrogen")){
                    totalNitrogen = Float.parseFloat(list.get(i).get(key));
                }else if(key.equals("总磷")||key.equals("totalPhosphorus")){
                    totalPhosphorus = Float.parseFloat(list.get(i).get(key));
                }else if(key.equals("亚硝酸盐")||key.equals("nitrite")){
                    nitrite = Float.parseFloat(list.get(i).get(key));
                    log.info(list.get(i).get(key));
                }else if(key.equals("高锰酸钾" )||key.equals("permanganate")){
                    permanganate = Float.parseFloat(list.get(i).get(key));
                }else if(key.equals("采集地点")){
                    pondId = list.get(i).get(key);
                }else{
                    date = simpleDateFormat.parse(list.get(i).get(key));
                }
            }
            FishPond fishPond = fishPondMapper.selectById(pondId);
            WaterFactor waterFactor = new WaterFactor(date,waterTemperature,PH,ammoniaNitrogen,totalNitrogen,totalPhosphorus,permanganate,nitrite,fishPond);
            if (waterFactorMapper.selectIdByPondIdAndCollectionTime(waterFactor) == null) {
                waterFactorMapper.insertWaterFactor(waterFactor);
            }else{
                waterFactorMapper.updateWaterFactor(waterFactor);
            }
        }
    }

    public List<WaterFactor> getByPondId(String pondId) {
        return waterFactorMapper.getByPondId(pondId);
    }

    public List<String> getPondId() {
        return waterFactorMapper.getPondId();
    }

    public List<WaterFactor> getWaterInfo(String el) {
        return waterFactorMapper.getWaterInfo(el);
    }

    public List<WaterFactor> getByPondIdAndDate(Map<String,Object> map) {
        return waterFactorMapper.getByPondIdAndDate(map);
    }

    public List<WaterFactor> getWaterInfoByDate(Date date) {
        return waterFactorMapper.getWaterInfoByDate(date);
    }
}
